package com.gdufe.firesafe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdufe.firesafe.dao.HouseDao;
import com.gdufe.firesafe.dao.OwnerDao;
import com.gdufe.firesafe.model.entity.HouseEntity;
import com.gdufe.firesafe.model.entity.OwnerEntity;
import com.gdufe.firesafe.exception.RRException;
import com.gdufe.firesafe.service.OwnerService;
import com.gdufe.firesafe.utils.PageUtils;
import com.gdufe.firesafe.utils.Query;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@AllArgsConstructor
@Service("ownerService")
public class OwnerServiceImpl extends ServiceImpl<OwnerDao, OwnerEntity> implements OwnerService {
    private HouseDao houseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OwnerEntity> page = this.page(
                new Query<OwnerEntity>().getPage(params),
                new QueryWrapper<OwnerEntity>()
        );

        return new PageUtils(page);
    }

    //根据手机号查找
    @Override
    public OwnerEntity selectByPhone(String phone) {
        return this.getOne(new QueryWrapper<OwnerEntity>().eq("phone",phone));
    }

    @Override
    public boolean save(OwnerEntity entity) {
        HouseEntity house=houseDao.selectById(entity.getHouseId());
        //检验房屋是否存在
        if(house==null){
            throw new RRException("该房屋不存在");
        }
        //检验房屋是否已有业主
        if(house.getOwnerId()!=null){
            throw new RRException("该房屋已有业主");
        }
        return super.save(entity);
    }

}
